import sys

from PyQt4.QtCore import Qt, QPointF, pyqtSignal
from PyQt4.QtGui import QPainter, QPolygonF, QSplitter, QSplitterHandle


class SplitterHandle(QSplitterHandle):
    clicked = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super(SplitterHandle, self).__init__(*args, **kwargs)
        self.setMouseTracking(True)
        self.parent = args[1]

    def mousePressEvent(self, event):
        super(SplitterHandle, self).mousePressEvent(event)
        if event.pos().y() <= 24:
            self.clicked.emit()

    def mouseMoveEvent(self, event):
        if event.pos().y() <= 24:
            self.unsetCursor()
            event.accept()
        else:
            self.setCursor(Qt.SplitHCursor if self.orientation() == Qt.Horizontal else Qt.SplitVCursor)
            super(SplitterHandle, self).mouseMoveEvent(event)

    def paintEvent(self, event):
        super(SplitterHandle, self).paintEvent(event)
        # painter = QPainter(self)
        # painter.setRenderHint(QPainter.Antialiasing, True)
        # painter.setPen(Qt.red)
        # painter.drawRect(0, 0, self.width(), 24)
        # painter.setBrush(Qt.red)
        # painter.drawPolygon(QPolygonF([
        #     QPointF(0, (24 - 8) / 2),
        #     QPointF(self.width() - 2, 24 / 2),
        #     QPointF(0, (24 + 8) / 2)
        # ]))


class Splitter(QSplitter):

    def __init__(self, *__args):
        QSplitter.__init__(self, *__args)
        self.parent = __args[0]

    def onClicked(self):
        print('clicked')

    def resizeEvent(self, event):
        pass
        # print(dir(event))
        # print(event.type())
        # print(event.size())
        # size = event.size()
        # width, height = size.width(), size.height()
        # self.parent.tab_resize(width, height)

    def createHandle(self):
        if self.count() >= 2:
            handle = SplitterHandle(self.orientation(), self)
            handle.clicked.connect(self.onClicked)
            return handle
        return super(Splitter, self).createHandle()
